home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Game Programming in C++ - Start to Finish
/
GameProgrammingS.iso
/
Peon
/
PeonSDK-Win32-1.0.0.exe
/
{app}
/
PeonMain
/
source
/
AudioEngine.cpp
next >
Wrap
C/C++ Source or Header
|
2005-11-07
|
5KB
|
248 lines
#include "FileLogger.h"
#include "IniConfigReader.h"
#include "AudioEngine.h"
namespace peon
{
template<> AudioEngine* ISingleton<AudioEngine>::ms_Singleton = 0;
AudioEngine* AudioEngine::getSingletonPtr(void)
{
return ms_Singleton;
}
AudioEngine& AudioEngine::getSingleton(void)
{
assert( ms_Singleton );
return ( *ms_Singleton );
}
AudioEngine::AudioEngine()
{
m_pALDevice = NULL;
m_pALContext = NULL;
m_bEnableSound = true;
m_bEnableMusic = true;
m_bAudioSupported = true;
m_iCurrentSlot = 0;
}
AudioEngine::~AudioEngine()
{
unloadEngine();
}
bool AudioEngine::loadEngine( IniConfigReader* pConfig )
{
int audio_rate = 22050;
Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
int audio_channels = 2;
int audio_buffers = 4096;
// This is where we open up our audio device.
// Note that this section is for loading up SDL_Mixer for
// MIDI support
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
{
FileLogger::getSingleton().logError("AudioEngine", "Error loading audio device");
m_bAudioSupported = false;
return false;
}
//alutInit(NULL, 0);
alGetError(); //clear error code
//open a link to the audio hardware using the DirectSound3D
//underlay
m_pALDevice = alcOpenDevice((ALubyte*)"DirectSound3D");
if(m_pALDevice == NULL)
{
m_bAudioSupported = false;
return false;
}
//Create a valid context
m_pALContext=alcCreateContext(m_pALDevice,NULL);
//make it the current active context
alcMakeContextCurrent(m_pALContext);
m_iCurrentSlot = 0;
alGenBuffers(NUM_BUFFERS, m_uAudioBuffers);
if(alGetError() != AL_NO_ERROR)
{
m_bAudioSupported = false;
return false;
}
//position of the listener
ALfloat ListenerPos[] = { 0.0f, 0.0f, 0.0f };
// Velocity of the listener.
ALfloat ListenerVel[] = { 0.0f, 0.0f, 0.0f };
//Orientation of the listener. (first 3 elements are "at", second 3 are "up")
ALfloat ListenerOri[] = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
alListenerfv(AL_POSITION, ListenerPos);
alListenerfv(AL_VELOCITY, ListenerVel);
alListenerfv(AL_ORIENTATION, ListenerOri);
return true;
}
void AudioEngine::unloadEngine()
{
Mix_HaltMusic();
Mix_CloseAudio();
//Get active context
m_pALContext = alcGetCurrentContext();
//Get device for active context
m_pALDevice=alcGetContextsDevice(m_pALContext);
//Disable context
alcMakeContextCurrent(NULL);
//Release context(s)
alcDestroyContext(m_pALContext);
//Close device
alcCloseDevice(m_pALDevice);
alutExit();
}
Mix_Music* AudioEngine::loadMIDI( const String& strFilename )
{
Mix_Music* pMusicChunk = NULL;
/* Actually loads up the music */
pMusicChunk = Mix_LoadMUS(strFilename.c_str());
return( pMusicChunk );
}
Mix_Chunk* AudioEngine::loadWAVChunk( const String& strFilename )
{
Mix_Chunk* pChunk = NULL;
pChunk = Mix_LoadWAV( strFilename.c_str() );
return( pChunk );
}
bool AudioEngine::loadAudioNode(const String& strWAV, AudioNode* pNode)
{
ALenum format;
ALsizei size;
ALvoid* data;
ALsizei freq;
ALboolean loop;
//String strTemp = strWAV;
FILE* oggFile; // file handle
OggVorbis_File oggStream; // stream handle
vorbis_info* vorbisInfo; // some formatting data
vorbis_comment* vorbisComment; // user comments
alGenSources( 1, &pNode->sound_source );
if (alGetError() != AL_NO_ERROR)
return false;
int pos = (int)strWAV.find( ".ogg", 0 );
if ( pos != String::npos )
{
int res;
//we're loading an OGG file!!!
if(!(oggFile = fopen(strWAV.c_str(), "rb")))
return false;
if((res = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
{
fclose(oggFile);
return false;
}
vorbisInfo = ov_info(&oggStream, -1);
vorbisComment = ov_comment(&oggStream, -1);
if(vorbisInfo->channels == 1)
format = AL_FORMAT_MONO16;
else
format = AL_FORMAT_STEREO16;
alSourcef(pNode->sound_source, AL_ROLLOFF_FACTOR, 0.0f );
alSourcei(pNode->sound_source, AL_SOURCE_RELATIVE, AL_TRUE );
}
else
{
//we're loading a simple ol' WAV file
alutLoadWAVFile((char*)strWAV.c_str(), &format, &data, &size, &freq, &loop);
alBufferData(m_uAudioBuffers[m_iCurrentSlot], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);
pNode->sound_buffer = m_iCurrentSlot;
}
m_iCurrentSlot++;
return true;
}
void AudioEngine::setAudioNode(AudioNode* pNode)
{
ALuint source = pNode->sound_source;
alSourcei (source, AL_BUFFER, m_uAudioBuffers[pNode->sound_buffer] );
alSourcef (source, AL_PITCH, 1.0f );
alSourcef (source, AL_GAIN, 1.0f );
alSourcefv(source, AL_POSITION, pNode->sound_position );
alSourcefv(source, AL_VELOCITY, pNode->sound_velocity );
if( pNode->sound_loop )
alSourcei (source, AL_LOOPING, AL_TRUE );
else
alSourcei (source, AL_LOOPING, AL_FALSE );
}
void AudioEngine::playAudioNode( AudioNode* pNode )
{
// Begin the source playing.
alSourcePlay( pNode->sound_source );
}
void AudioEngine::stopAudioNode( AudioNode* pNode )
{
alSourceStop( pNode->sound_source );
}
}